package com.bysc.userservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.userservice.entity.vo.CollegeAndMajorVo;
import com.bysc.userservice.entity.UserCollege;
import com.bysc.userservice.entity.UserMajor;
import com.bysc.userservice.entity.vo.CollegeQueryVo;
import com.bysc.userservice.service.UserCollegeService;
import com.bysc.userservice.service.UserMajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-05-20
 */
@Api(tags = "学院管理")
@RestController
@RequestMapping("/userservice/user-college")
public class UserCollegeController {
    @Autowired
    private UserCollegeService userCollegeService;


    @ApiOperation(value = "新增学院")
    @PostMapping("addCollege")
    @CacheEvict(value = "index", key = "'CollegeAndMajor'", allEntries = true)
    public Results addMajorByCollegeId(@ApiParam(name = "CollegeInfo", value = "学院信息", required = true) @RequestBody UserCollege college) {
        try {
            userCollegeService.save(college);
            return Results.ok().message(MessageConstant.ADD_COLLEGE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.ADD_COLLEGE_FAIL);
        }
    }

    @ApiOperation(value = "根据学院id删除学院")
    @CacheEvict(value = "index", key = "'CollegeAndMajor'", allEntries = true)
    @DeleteMapping("deleteCollegeById/{id}")
    public Results deleteCollegeById(@ApiParam(name = "id", value = "学院id", required = true) @PathVariable String id) {
        try {
            UserCollege college = userCollegeService.getById(id);
            if (college != null) {
                userCollegeService.removeById(id);
            }
            return Results.ok().message(MessageConstant.DELETE_COLLEGE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.DELETE_COLLEGE_FAIL);
        }
    }

    @ApiOperation(value = "根据学院id修改学院信息")
    @CacheEvict(value = "index", key = "'CollegeAndMajor'", allEntries = true)
    @PutMapping("updateCollegeById/{id}")
    public Results updateCollegeById(@ApiParam(name = "CollegeInfo", value = "学院信息", required = true) @RequestBody UserCollege userCollege) {
        try {
            userCollegeService.updateById(userCollege);
            return Results.ok().message(MessageConstant.DELETE_COLLEGE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.EDIT_COLLEGE_FAIL);
        }
    }

    @ApiOperation(value = "根据学院id查找学院信息")
    @GetMapping("getCollegeById/{id}")
    public Results getCollegeById(@ApiParam(name = "id", value = "学院id", required = true) @PathVariable String id) {
        try {
            UserCollege college = userCollegeService.getById(id);
            return Results.ok().data("college", college).message(MessageConstant.QUERY_COLLEGE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.QUERY_COLLEGE_FAIL);
        }
    }

    @ApiOperation(value = "查询所有学院及其专业数据")
    @Cacheable(value = "index", key = "'CollegeAndMajor'")
    @GetMapping("getAllCollegeAndMajor")
    public Results getAllCollegeAndMajor() {
        try {
            List list = userCollegeService.getAllCollegeAndMajor();
            return Results.ok().data("collegeAndMajor", list).message(MessageConstant.QUERY_COLLEGE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_COLLEGE_FAIL);
        }
    }

    @ApiOperation(value = "查询所有学院信息")
    @GetMapping("getAllCollege")
    public Results getAllCollege() {
        try {
            List<UserCollege> collegeList = userCollegeService.list(new QueryWrapper<UserCollege>().orderByDesc("sort"));
            return Results.ok().data("collegeList", collegeList).message(MessageConstant.QUERY_COLLEGE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.SUCCESS, MessageConstant.QUERY_COLLEGE_FAIL);
        }
    }
    @ApiOperation(value = "带条件分页查询学院")
    @PostMapping("getCollegeByQueryVo/{page}/{limit}")
    public Results getCollegeByQueryVo(@ApiParam(name = "page", value = "查询页数", required = true)
                                     @PathVariable Integer page,@ApiParam(name = "limit", value = "查询数量", required = true)
                                     @PathVariable Integer limit,@ApiParam(name = "queryVo", value = "查询条件", required = false)
    @RequestBody CollegeQueryVo queryVo) {
        try {
            Page<UserCollege> infoPage = new Page<>(page,limit);
            String name = queryVo.getName();
            LambdaQueryWrapper<UserCollege> wrapper = new LambdaQueryWrapper<>();

            if (!StringUtils.isEmpty(name)){
                wrapper.like(UserCollege::getName,name);
            }
            wrapper.orderByDesc(UserCollege::getSort);
            IPage<UserCollege> iPage = userCollegeService.page(infoPage, wrapper);
            return Results.ok().data("list",iPage).message(MessageConstant.QUERY_COLLEGE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_COLLEGE_FAIL);
        }
    }
}

